<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>圣诞惊喜</title>
    <link rel="stylesheet"  href="src/css/base.css?__version=9.4">
    <link rel="stylesheet"  href="src/css/pages.css?__version=9.4">
    <script type="text/javascript" src="src/js/jquery-1.10.1.min.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/rem.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/dropload.js?__version=9.4"></script>
    <script type="text/javascript" src="src/js/init.js?__version=9.4"></script>
	<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
	<script src="/src/js/share.js?__version=9.4"></script>
	<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?8ed2efe5b24e14f351f4d88ba2eb3462";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
    </script>
</head>
<body>
	<div class="init_content" style="display: none;">
    <div class="warp backpack-warp activity_warp">
 		<img style="width: 100%;height: 100%" src="/src/img/hd_2.jpg">
        <section class="caption" id="htmlDataList"></section>
        <div class="submit"></div>
    </div>
	</div>
    <div class="init_loading">
        <img src="src/img/page_loading.gif?v=1">
    </div>
</body>
<script>

$(function(){


	var token = getQueryString("token");
	var isapp = getQueryString("isApp");
	if(isapp!=1){
		addheader("圣诞惊喜","/");
	}else{
		$(".activity_warp").css("top","0px")
	}
	if(token) {
		getAll();
	}
});
function getAll(){
	$.ajax({
        type:"post",
        url:api_url+"/OpenAPI/V1/Backpack/getAll",
        jsonp:'callback',
        dataType:'jsonp',
        data:{page:1,token:token},
        success:function(data){
        	console.log(data)
        	var htmlDataList="";
        	var data=data.data;
			if(data.list.length>0){
				for(var i=0;i<data.list.length;i++){
					if(data.list[i]['toy_type']!='1'&&data.list[i]['toy_type']){
						htmlDataList += '<label class="item flex">';
						htmlDataList += '<img src='+data.list[i].cover+' width="50px;" heoght="50px;">';
						htmlDataList += '<div class="info">';
						htmlDataList += '<p class="name text-beyond">'+data.list[i].title+'<span>兑换'+data.list[i].to_user_price+'金币</span></p>';
						htmlDataList += '<p class="time text-beyond">'+data.list[i].create_at+'</p>';
						htmlDataList += '</div>';
						htmlDataList += '<span class="align-right ">';
						htmlDataList += '<input class="icon-check" type="checkbox" name="ids[]" value="'+data.list[i].backpack_id+'">';
						htmlDataList += '<input class="icon-check" type="hidden" id="toy_'+data.list[i].backpack_id+'" value="'+data.list[i].toy_id+'">';
						htmlDataList += '<input class="icon-check" type="hidden" id="money_'+data.list[i].backpack_id+'" value="'+data.list[i].to_user_price+'">';
						htmlDataList += '<input class="icon-check" type="hidden" id="name_'+data.list[i].backpack_id+'" value="'+data.list[i].title+'">';
						htmlDataList += '<input class="icon-check" type="hidden" id="img_'+data.list[i].backpack_id+'" value="'+data.list[i].cover+'">';
						htmlDataList += '</span>';
						htmlDataList += '</label>';
					}
				}
			}else{
				htmlDataList="<div class='nulldefault'><img src='src/img/nulldefault.png'><p>暂无普通礼物哦～</p></div>"
			}
			$("#htmlDataList").html(htmlDataList);
			if($("#htmlDataList .item").length>0){
				$(".activity_warp .submit").html('<button type="button" class="btn btn-yellow" style="color:#666;" onclick="operation()">兑换圣诞礼物</button>')
			}
	        }
    })
}




function operation(type){
	var back_ids = new Array();
	var toy_ids = new Array();
	var toys = new Array();
	$("input[name='ids[]']:checked").each(function(i){//把所有被选中的复选框的值存入数组
		var back_id = $(this).val();//背包ID
		var toy_id = $('#toy_'+back_id).val();//娃娃ID
		var money = $('#money_'+back_id).val();//兑换金额
		var name = $('#name_'+back_id).val();//娃娃昵称
		var img = $('#img_'+back_id).val();//娃娃图片
		
		if(jQuery.inArray(toy_id,toy_ids) >= 0){
			for(var i=0;i<toys.length;i++){
				if(toys[i]["toy_id"]==toy_id){
					toys[i]["money"]=Number(toys[i]["money"])+Number(money);
					toys[i]["num"]=Number(toys[i]["num"])+Number(1);
				}
			}
		}else{
			toys.push({'toy_id':toy_id,'money':money,'name':name,'img':img,'num':1});
			toy_ids.push(toy_id);
		}
		
		back_ids.push(back_id);
    });
	
	if(back_ids.length == 0){
		alert('请选择');
		return;
	}
	$.ajax({
        type:"post",
        url:api_url+"/OpenAPI/V1/Activity/activityExchange",
        jsonp:'callback',
        dataType:'jsonp',
        data:{page:1,token:token,back_ids:back_ids},
        success:function(data){
        	if(data.code==1){
        		alert(data.msg);
        	}else{
        		alert(data.data);
        		getAll();
        	}
        }
    })
}
</script>
</html>